<script setup>
import ComAffix from "@/components/ComAffix.vue";
import { User, Star } from '@element-plus/icons-vue';
import {ref, computed} from 'vue';
import router from '@/router/index.js'
import {searchUser} from "@/api/search.js";
import {useUserInfoStore, useUserStore} from "@/stores/index.js";
import {follow} from "@/utiles/userInfo.js";
const userInfoStore = useUserInfoStore()
const useStore = useUserStore()
const route = computed(() => router.currentRoute.value);
const nickname = route.value.query.nickname

const handleFollow = (sign) => {
  console.log("关注")
  follow(Number(useStore.user.id), sign)
}

const users = ref([
])
const getUsers = async () =>{
  const  res = await searchUser(route.value.query.nickname)
  users.value = res.data.data
}
getUsers()



</script>

<template>
  <div class="main">
    <el-affix :offset="0">
      <ComAffix></ComAffix>
    </el-affix>
    <div class="w">
      <!-- 导航区域 -->
      <div class="search-nav">
        <div class="nav-left">
          <span><router-link  :to="{ name: 'SearchPost', query: { searchName: nickname} }">文章</router-link></span>
          <span>
             <router-link :to="{ name: 'SearchTag', query: { tagName: nickname} }">标签</router-link>
          </span>
          <span class="active">用户</span>
        </div>
      </div>


      <!-- 用户列表 -->
      <div class="user-list">
        <el-card
          v-for="user in users"
          :key="user.id"
          shadow="hover"
          class="user-card"
        >
          <div class="user-container">
            <!-- 左侧信息 -->
            <div class="user-main">
              <el-avatar
                :size="60"
                :src="user.avatar"
                class="user-avatar"
              >
                <template #default>
                  <User />
                </template>
              </el-avatar>

              <div class="user-info">
                <div class="username">
                  {{ user.nickname}}
                  <el-icon v-if="user.isVip" color="#e5a454" class="vip-icon">
                    <Star />
                  </el-icon>
                </div>
                <div class="bio">{{ user.introduced }}</div>
                <div class="stats">
                  <span>粉丝 {{ user.followee }}</span>
                  <el-divider direction="vertical" />
                  <el-divider direction="vertical" />
                  <span>作品 {{ user.postCount}}</span>
                </div>
              </div>
            </div>

            <!-- 右侧操作 -->
            <div class="follow">
              <span class="followed" v-if="userInfoStore.follow.includes(Number(useStore.user.id))"
                    @click="handleFollow(1)">已关注</span>
              <span class="follow" v-else @click="handleFollow(0)">关注</span>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.main {
  min-height: 100vh;
  background-color: #f6f3f3;
}

.w {
  width: 1000px;
  margin: 0 auto;
}

.search-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 30px 0 ;
  padding: 20px 20px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.05);

  .nav-left {
    span {
      margin-right: 30px;
      cursor: pointer;
      color: #666;
      padding: 8px 12px;
      border-radius: 4px;

      &.active {
        background: #f0f2f5;
        color: #409eff;
        font-weight: 500;
      }

      &:hover {
        background: #f5f7fa;
      }
    }
  }

  .active {
    background: #f0f2f5;
    color: #409eff;
    font-weight: 500;
  }
  a {
    text-decoration: none;
    cursor: pointer;
    color: #636363;
  }
}

.user-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.user-card {
  margin-bottom: 12px;

  .user-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
  }

  .user-main {
    display: flex;
    align-items: center;
    gap: 20px;
  }

  .user-info {
    .username {
      font-size: 18px;
      font-weight: 500;
      margin-bottom: 6px;
      display: flex;
      align-items: center;
      gap: 8px;

      .vip-icon {
        font-size: 16px;
      }
    }

    .bio {
      color: #666;
      font-size: 0.9em;
      margin-bottom: 8px;
    }

    .stats {
      color: #888;
      font-size: 0.85em;
      display: flex;
      align-items: center;
      gap: 12px;

      ::v-deep .el-divider--vertical {
        margin: 0;
        height: 1em;
      }
    }
  }
}
</style>
